package com.bigtotoro.shuiyincamera.my_arts;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bigtotoro.shuiyincamera.R;
import com.bigtotoro.shuiyincamera.model.MyArt;
import com.bigtotoro.shuiyincamera.util.DimenUtils;
import com.bigtotoro.shuiyincamera.ImageDisplayActivity;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.luck.picture.lib.decoration.RecycleViewDivider;

import org.litepal.LitePal;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 视频
 */
public class MyVideosFragment extends Fragment {
    private View rootview;
    private List<MyArt> mPhotoList;
    private RecyclerView recyclerView;

    private int type = 1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootview == null) {
            rootview = inflater.inflate(R.layout.fragment_my_photos, null);

            initData();
        }
        ViewGroup parent = (ViewGroup) rootview.getParent();
        if (parent != null) {
            parent.removeView(rootview);
        }
        return rootview;
    }

    private void initGrid() {
        recyclerView = rootview.findViewById(R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.HORIZONTAL, DimenUtils.dip2px(getActivity(), 1), Color.parseColor("#eeeeee")));
        recyclerView.setAdapter(new BaseQuickAdapter<MyArt, BaseViewHolder>(
                R.layout.item_grid, mPhotoList){
            @Override
            protected void convert(BaseViewHolder helper, MyArt item) {
                helper.setText(R.id.item_time, "最后修改时间: " + item.getCreate_time())
                        .setText(R.id.item_path, "文件路径: " + item.getPath())
                        .addOnClickListener(R.id.iv_photo);
                if (item.getPath() != null && item.getPath().lastIndexOf(".") > 0) {
                    Glide.with(getActivity()).load(new File(item.getPath())).into((ImageView) helper.getView(R.id.iv_photo));
                }

                ImageView iv_cover = helper.getView(R.id.iv_cover);
                switch (item.getType()){  //根据类型显示不同的标签
                    case 0:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.menu01));
                        break;
                    case 1:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.menu02));
                        break;
                    case 2:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.menu03));
                        break;
                    case 3:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.video_qushuiyin));
                        break;
                    case 4:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.video_jiashuiyin));
                        break;
                    case 5:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.video_pinjie));
                        break;
                    case 6:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.video_jiancai));
                        break;
                    case 7:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.video_jianji));
                        break;
                    case 8:
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.yinyue));
                        break;
                }
            }
        });
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ArrayList<String> paths = new ArrayList<>();
                for (MyArt item : mPhotoList) {
                    paths.add(item.getPath());
                }
                startActivity(new Intent(getActivity(),
                        ImageDisplayActivity.class)
                        .putStringArrayListExtra("images", paths)
                        .putExtra("local", true)
                        .putExtra("index", position)
                );
            }
        });
    }

    private void openAssignFolder(String path) {
        File file = new File(path);
        if (null == file || !file.exists()) {
            return;
        }
        //获取父目录  
        File parentFlie=new File(file.getParent());
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.fromFile(new File(parentFlie.getPath())));
//        intent.putExtra("org.openintents.extra.ABSOLUTE_PATH", folder.path);
        if (intent.resolveActivity(getContext().getPackageManager()) != null) {
            getContext().startActivity(intent);
        }
    }

    private void initData() {
        mPhotoList = LitePal.where("type=?", ""+type).find(MyArt.class);
        if (mPhotoList != null && mPhotoList.size() > 0) {
            initGrid();
        } else {
            Toast.makeText(getActivity(), "暂无数据", Toast.LENGTH_SHORT).show();
        }
    }
}
